/**
 * 获取所有的书列表
 */
const superagent = require('superagent');
const { website} = require('../config');
const bookModel = require('../model/bookModel');
const { getChapter } = require('../controls/chapter');
const cheerio = require('cheerio');
const logger = require('../config/log4');
/**
 * 获取所有的书本列表
 */
const getAllBooks = (page,total) => {
    getChapter();
    return;
    superagent
    .get(`${website.baseurl}paihangbang/`)
    .end((err,res)=>{
        if(err)
            throw err;
        const $ = cheerio.load(res.text);
        if(saveAllBooks($))
            //存储完成
            getChapter();        
    })
}
/**
 * 解析获取的页面数据
 * @param {*} $ 
 */
const saveAllBooks = ($) => {
    const list = $(".index_toplist");//类型
    list.each((index,element) => {
        const bookList = $($(element).find('.topbooks')[1]).find('li');//当前类型的排行榜
        let type = $(element).find('.toptab span')[0].childNodes[0].data;
        type = type.substr(0,type.length - 2 );
        let data = [];
        bookList.each((i,el)=>{
            const elD = $(el).find('a');
            data.push({
                title: elD[0].attribs.title,
                url: elD[0].attribs.href,
                type: type,
                id: elD[0].attribs.href.split("/")[2]
            })
        })
        bookModel.save(data); 
    });
    logger.info('当前页保存完成'); 
    return true;
}
module.exports = {
    getAllBooks
}